//
//  UITextView+XMPlaceholder.m
//  XMKit
//
//  Created by chenming on 2017/12/21.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "UITextView+XMPlaceholder.h"

@implementation UITextView (XMPlaceholder)

@dynamic xm_placeholder;

static const NSString *KEY_XM_PLACEHOLDER = @"XMPlaceholder";

- (void)setXm_placeholder:(UILabel *)xm_placeholder {
    objc_setAssociatedObject(self, &KEY_XM_PLACEHOLDER, xm_placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)xm_placeholder {
    return objc_getAssociatedObject(self, &KEY_XM_PLACEHOLDER);
}

- (void)addPlaceholderLabelText:(NSString *)text {
    if (!self.xm_placeholder) {
        self.xm_placeholder = [[UILabel alloc] init];
        self.xm_placeholder.font = self.font;
        self.xm_placeholder.textColor = [UIColor xm_colorWithHexString:@"999999" alpha:1];
        [self addSubview:self.xm_placeholder];
        [self setValue:self.xm_placeholder forKey:@"_placeholderLabel"];
    }
    self.xm_placeholder.text = text;
    [self.xm_placeholder sizeToFit];
    [self setNeedsLayout];
}

// 容易引起布局不准确
//- (void)layoutSubviews {
//    [super layoutSubviews];
//    [self.xm_placeholder mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self).offset(5);
//        make.left.mas_equalTo(self).offset(5);
//    }];
//    [self setNeedsLayout];
//}

@end
